//
//  BeseHttpClient.m
//  BJYShoppingMall
//
//  Created by Coder on 15/10/10.
//  Copyright © 2015年 BJY12. All rights reserved.
//

#import "BeseHttpClient.h"

@implementation BeseHttpClient
@synthesize delegate;



/**
 *  获取评论的订单详情
 */
-(void)GetTradeApplicationByTradeId:(NSString*)userCookies tradeid:(NSString*)tradeId
{
    requestType = HTTP_GetTradeApplicationByTradeId;
    ASIFormDataRequest *request = [self createRequest:@"/app/GetTradeApplicationByTradeId"];
    [request addPostValue:userCookies          forKey:@"userCookies"];
    [request addPostValue:tradeId              forKey:@"tradeId"];
    
    [request startAsynchronous];
    [self getJsonDataForArray:request];
}

/**
 *  获取二维码链接
 */
-(void)GetDealQrcode:(NSString*)userCookies code:(NSString*)code
{
    requestType = HTTP_DealQrcode;
    ASIFormDataRequest *request = [self createRequest:@"/app/DealQrcode"];
    [request addPostValue:userCookies          forKey:@"userCookies"];
    [request addPostValue:code                 forKey:@"code"];
    
    [request startAsynchronous];
    [self getJsonDataForArray:request];
}


/**
 *  上传头像至服务器
 *
 *  @param imgData     图片的二进制
 *  @param uid    用户ID
 */
-(void)UploadHeadImg:(NSData*)imgData andUID:(NSString*)uid
{
    requestType = HTTP_UploadHeadImg;
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible= YES;
    NSString * url = [NSString stringWithFormat:@"%@%@",@"http://f.zlc99.com/FileUpload/UploadImages.aspx?uid==",uid];
    NSLog(@"--url:%@",url);
    
    ASIFormDataRequest *_request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:url]];
    _request.timeOutSeconds= 30L;
    [_request setRequestMethod:@"POST"];
    
    [_request addData:imgData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"headImage"];
    [_request startAsynchronous];
    
    [self getJsonData:_request];
    
    
}
/**
 *  晒单评论
 */
-(void)AddTradeComment:(NSString*)userCookies tradeId:(NSString*)tradeId experience:(NSString*)experience Img1:(NSString*)Img1 Img2:(NSString*)Img2 Img3:(NSString*)Img3 score:(NSString*)score
{
    requestType = HTTP_AddTradeComment;
    ASIFormDataRequest *request = [self createRequest:@"/app/AddTradeComment"];
    [request addPostValue:userCookies             forKey:@"userCookies"];
    [request addPostValue:tradeId                 forKey:@"tradeId"];
    [request addPostValue:Img1                    forKey:@"img1"];
    [request addPostValue:Img2                    forKey:@"img2"];
    [request addPostValue:Img3                    forKey:@"img3"];
    [request addPostValue:experience              forKey:@"experience"];
    [request addPostValue:score                   forKey:@"score"];
    
    [request startAsynchronous];
    [self getJsonDataForArray:request];
}



#pragma mark - Base Functions 公共发起请求的方法

/**
 *  创建请求的基础方法
 *
 *  @param urlPath 请求地址不含域名的部分
 *
 *  @return ASIFormDataRequest对象
 */
-(ASIFormDataRequest *)createRequest:(NSString *)urlPath
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible= YES;
    NSString * url = [NSString stringWithFormat:@"%@%@",MY_SDK_URL,urlPath];
    ASIFormDataRequest *_request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:url]];
    _request.timeOutSeconds= 30L;
    [_request setRequestMethod:@"POST"];
    return _request;
}

/**
 *  创建请求的基础方法，使用全URL路径
 *
 *  @param url 全URL路径
 *
 *  @return ASIFormDataRequest对象
 */
-(ASIFormDataRequest *)createRequestByFullURL:(NSString *)url
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible= YES;
    ASIFormDataRequest *_request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:url]];
    _request.timeOutSeconds= 30L;
    [_request setRequestMethod:@"POST"];
    return _request;
}


-(void)getJsonData:(ASIFormDataRequest*)request
{
    __weak ASIFormDataRequest *requested = request;
    
    [requested setCompletionBlock :^{
        // 请求响应结束，返回 responseString
        NSData *responseData = [requested responseData];
        
        NSString *result = [[NSString alloc] initWithData:responseData  encoding:NSUTF8StringEncoding];
//        NSLog(@"--%@--返回resultString参数：%@",requestType,result);

        NSError *error = nil;
        NSMutableDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:
                                    [result dataUsingEncoding:NSUTF8StringEncoding]
                                                                   options: NSJSONReadingMutableContainers
                                                                     error: &error];
        
//        NSMutableDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:nil];
        
        NSLog(@"--%@--返回true参数：%@",requestType,resultDic);
        
        if (delegate) {
            [delegate HttpRequestSuccess:resultDic requestType:requestType];
        }
        
    }];
    
    
    [requested setFailedBlock :^{
        // 请求响应失败，返回错误信息
        NSError *error = [requested error ];
        NSLog(@"--%@--error---%@",requestType,[error userInfo]);
        if (delegate) {
            [delegate HttpRequestError:error];
        }
    }];
    
    
}

-(void)getJsonDataForArray:(ASIFormDataRequest*)request
{
    __weak ASIFormDataRequest *requested = request;
    
    
    [requested setCompletionBlock :^{
        // 请求响应结束，返回 responseString
        NSData *responseData = [requested responseData];
        NSMutableArray *resultArray = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:nil];
        NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
        [resultDic setValue:resultArray forKey:ReturnArray];
        NSLog(@"--%@--返回true参数：%@",requestType,resultDic);
        
        if (delegate) {
            [delegate HttpRequestSuccess:resultDic requestType:requestType];
        }
        
        
    }];
    
    
    [requested setFailedBlock :^{
        // 请求响应失败，返回错误信息
        NSError *error = [requested error ];
        NSLog(@"--%@--error---%@",requestType,[error userInfo]);
        if (delegate) {
            [delegate HttpRequestError:error];
        }
    }];
}


-(void)getJsonDataForString:(ASIFormDataRequest*)request
{
    __weak ASIFormDataRequest *requested = request;
    
    
    [requested setCompletionBlock :^{
        // 请求响应结束，返回 responseString
        NSData *responseData = [requested responseData];
        NSString *dataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
        [resultDic setValue:dataString forKey:ReturnResult];
        NSLog(@"--%@--返回true参数：%@",requestType,resultDic);
        
        if (delegate) {
            [delegate HttpRequestSuccess:resultDic requestType:requestType];
        }
        
        
    }];
    
    
    [requested setFailedBlock :^{
        // 请求响应失败，返回错误信息
        NSError *error = [requested error ];
        NSLog(@"--%@--error---%@",requestType,[error userInfo]);
        if (delegate) {
            [delegate HttpRequestError:error];
        }
    }];
}

@end
